package com.getpebble.android.jskit.bridge;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.jskit.webapps.JsKit;
import java.util.UUID;

public class JsAppLifecycleListener extends BroadcastReceiver {
    private static final String TAG = JsAppLifecycleListener.class.getSimpleName();

    public void onReceive(Context context, Intent intent) {
        JsAppContextBridge.getInstance().setAppContext(context);
        String appUUIDString = intent.getStringExtra("uuid");
        if (TextUtils.isEmpty(appUUIDString)) {
            Trace.info(TAG, "onReceive: null/empty uuid");
            return;
        }
        UUID appUUID = UUID.fromString(appUUIDString);
        if (intent.getAction().equals("com.getpebble.android.jskit.watchapplifecycle")) {
            if (intent.getBooleanExtra("started", false)) {
                handleWatchAppStarted(context, appUUID);
            } else {
                handleWatchAppStopped(context, appUUID);
            }
        } else if (intent.getAction().equals("com.getpebble.android.jskit.launch_config")) {
            handleConfigLaunchRequest(context, appUUID);
        }
    }

    public static void onWatchAppStarted(Context context, UUID appUUID) {
        postWatchAppLifecycleEvent(context, appUUID, true);
    }

    public static void onWatchAppStopped(Context context, UUID appUUID) {
        postWatchAppLifecycleEvent(context, appUUID, false);
    }

    public static void onConfigLaunchRequest(Context context, UUID appUUID) {
        postConfigLaunchRequest(context, appUUID);
    }

    protected static void postWatchAppLifecycleEvent(Context context, UUID appUUID, boolean started) {
        Intent startIntent = new Intent(context, JsAppLifecycleListener.class);
        startIntent.setAction("com.getpebble.android.jskit.watchapplifecycle");
        startIntent.putExtra("uuid", appUUID.toString());
        startIntent.putExtra("started", started);
        Trace.verbose(TAG, "Posting com.getpebble.android.jskit.watchapplifecycle / started = " + started);
        context.sendBroadcast(startIntent);
    }

    protected static void postConfigLaunchRequest(Context context, UUID appUUID) {
        Intent startIntent = new Intent(context, JsAppLifecycleListener.class);
        startIntent.setAction("com.getpebble.android.jskit.launch_config");
        startIntent.putExtra("uuid", appUUID.toString());
        context.sendBroadcast(startIntent);
    }

    public void handleWatchAppStarted(Context context, UUID appUUID) {
        Trace.verbose(TAG, "handleWatchAppStarted: " + appUUID);
        JsKit.jsKitAccess().onWatchAppStarted(context, appUUID);
    }

    public void handleWatchAppStopped(Context context, UUID appUUID) {
        Trace.verbose(TAG, "handleWatchAppStopped: " + appUUID);
        JsKit.jsKitAccess().onWatchAppStopped(context, appUUID);
    }

    public void handleConfigLaunchRequest(Context context, UUID appUUID) {
        Trace.verbose(TAG, "handleConfigLaunchRequest: " + appUUID);
        JsKit.jsKitAccess().onSettingsLaunch(context, appUUID);
    }
}
